package com.hstc.controller;
import com.hstc.utils.DataTimeTools;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.model.tool.ToolCallingChatOptions;
import org.springframework.ai.support.ToolCallbacks;
import org.springframework.ai.tool.ToolCallback;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
public class ToolCallingController {
    @Resource
    private ChatModel chatModel;
    @Resource
    private ChatClient chatClient;

    @GetMapping("/toolcalling/chat")
    public String rag(@RequestParam(name = "msg",defaultValue = "你是谁现在几点") String msg){
//        1.工具注册到工具集合里
        ToolCallback[] tools  = ToolCallbacks.from(new DataTimeTools());
//        2.将工具集合配置进ChatOptions对象
        ToolCallingChatOptions options = ToolCallingChatOptions.builder().toolCallbacks(tools).build();
//        3.构建提示词
        Prompt prompt = new Prompt(msg, options);
//        4.调用大模型
        return chatModel.call(prompt).getResult().getOutput().getText();
    }


    @GetMapping("/toolcalling/chatclient")
    public Flux<String> chatClientRag(@RequestParam(name = "msg",defaultValue = "你是谁现在几点") String msg){
        return chatClient.prompt(msg)
                .tools(new DataTimeTools())
                .stream()
                .content();
    }
}
